package com.tarzan.edu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarzan.common.core.base.Pagination;
import com.tarzan.edu.dao.EduCourseMapper;
import com.tarzan.edu.dao.EduTeacherMapper;
import com.tarzan.edu.entity.EduCourse;
import com.tarzan.edu.entity.EduTeacher;
import com.tarzan.edu.entity.api.EduCourseApiResp;
import com.tarzan.edu.entity.api.IndexVO;
import com.tarzan.edu.entity.request.EduCourseIdReq;
import com.tarzan.edu.entity.request.EduCourseReq;
import com.tarzan.edu.entity.response.ChapterVo;
import com.tarzan.edu.entity.response.CoursePublishVo;
import com.tarzan.edu.entity.response.EduCourseResp;
import com.tarzan.edu.entity.response.EduTeacherResp;
import com.tarzan.edu.service.IEduChapterService;
import com.tarzan.edu.service.IEduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author liujinshan
 * @title: EduCourseServiceImpl
 * @projectName com.tarzan.edu.service.impl
 * @description: TODO
 * @date 2021/5/25 16:55
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements IEduCourseService {

    @Autowired
    private EduCourseMapper eduCourseMapper;

    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    @Autowired
    private IEduChapterService iEduChapterService;


    @Override
    public String insert(EduCourse eduCourse) {
        baseMapper.insert(eduCourse);
        return eduCourse.getId();
    }

    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    @Override
    public IPage<EduCourseResp> findCoursePageInfo(EduCourseReq eduCourseReq) {
        Long currentPage = Optional.of(eduCourseReq).map(EduCourseReq::getPagination).map(Pagination::getCurrentPage).orElse(null);
        Long pageSize = Optional.of(eduCourseReq).map(EduCourseReq::getPagination).map(Pagination::getPageSize).orElse(null);
        IPage page = new Page(currentPage,pageSize);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        page = baseMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public EduCourseResp findCourseById(String id) {
        EduCourseResp resp = new EduCourseResp();
        EduCourse eduCourse = baseMapper.selectById(id);
        BeanUtils.copyProperties(eduCourse,resp);
        return resp;
    }

    @Override
    public void update(EduCourse eduCourse) {
        baseMapper.updateById(eduCourse);
    }

    /**
     * @param id
     * @return java.lang.Object
     * @title findCoursePublishById
     * @description TODO 根据课程id查询相关信息
     * @author liujinshan
     * @data 2021/8/17 10:40
     */
    @Override
    public CoursePublishVo findCoursePublishById(String id) {
        CoursePublishVo coursePublishVo = new CoursePublishVo();
        coursePublishVo = eduCourseMapper.findCoursePublishById(id);
        return coursePublishVo;
    }

    /**
     * @param id
     * @return void
     * @title publishCourse
     * @description TODO 发布课程
     * @author liujinshan
     * @data 2021/8/17 10:53
     */
    @Override
    public void publishCourse(EduCourseIdReq req) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(req.getId());
        eduCourse.setStatus("Normal");
        baseMapper.updateById(eduCourse);
    }

    /**
     * @return java.lang.Object
     * @title index
     * @description TODO 获取 课程、教师信息 在首页展示
     * @author liujinshan
     * @data 2021/8/17 15:20
     */
    @Override
    public IndexVO index() {
        IndexVO indexVO = new IndexVO();
        //查询前8条热门课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> list =  eduCourseMapper.selectList(wrapper);
        //查询前4条名师
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        List<EduTeacher> lists =   eduTeacherMapper.selectList(wrapperTeacher);
        indexVO.setEduCourseResp(JSON.parseArray(JSON.toJSONString(list),EduCourseResp.class));
        indexVO.setEduTeacherResp(JSON.parseArray(JSON.toJSONString(lists), EduTeacherResp.class));
        return indexVO;
    }

    /**
     * @param id
     * @return com.tarzan.edu.entity.api.EduCourseApiResp
     * @title findApiCourseById
     * @description TODO
     * @author liujinshan
     * @data 2021/8/20 9:34
     */
    @Override
    public EduCourseApiResp findApiCourseById(String id) {
        EduCourseResp eduCourseResp = eduCourseMapper.findBaseCourseInfo(id);

        List<ChapterVo> chapterVoList = iEduChapterService.getChapterVideoByCourseId(id);

        EduCourseApiResp eduCourseApiResp = new EduCourseApiResp();
        eduCourseApiResp.setEduCourseResp(eduCourseResp);
        eduCourseApiResp.setChapterVoList(chapterVoList);
        return eduCourseApiResp;
    }
}
